package com.woniu.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.orderInfo.OrderInfoException;
import com.woniu.product.model.MemberCard;
import com.woniu.product.dao.MemberCardDao;
import com.woniu.product.param.AddCardParam;
import com.woniu.product.param.UpdateCardParam;
import com.woniu.product.param.memberCard.LoadMemberCardParam;
import com.woniu.product.service.MemberCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardDao, MemberCard> implements MemberCardService {
    @Resource
    private MemberCardDao memberCardDao;

    /**
     * 新增会员卡
     * @param param
     * @throws Exception
     */
    @Override
    public void addCard(AddCardParam param) throws Exception {
        System.out.println(param);
        if (param.getCardName() == null || param.getCardName().isEmpty()) {
            throw new IllegalArgumentException("会员卡名不能为空");
        }
        if (param.getCardType() == null || (param.getCardType() < 1 || param.getCardType() > 3)) {
            throw new IllegalArgumentException("会员卡类型无效");
        }
        if (param.getCardPrice() == null || param.getCardPrice().compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("会员卡价格无效");
        }
        if (param.getCardLimit() == null || param.getCardLimit() < 0) {
            throw new IllegalArgumentException("会员卡额度无效");
        }
        if (param.getCardLifespanDay() == null && param.getCardLifespanMonth() == null) {
            throw new IllegalArgumentException("会员卡有效期不能为空");
        }
        MemberCard memberCard = new MemberCard();
        memberCard.setCardLimit(param.getCardLimit());
        memberCard.setCardPrice(param.getCardPrice());
        memberCard.setCardType(param.getCardType());
        memberCard.setCardLifespanDay(param.getCardLifespanDay());
        memberCard.setCardLifespanMonth(param.getCardLifespanMonth());
        memberCard.setCardName(param.getCardName());
        memberCardDao.insert(memberCard);

    }
    /**
     * 更新会员卡信息
     * @param
     * @throws Exception
     */

    @Override
    public void update(UpdateCardParam param) throws Exception {
        if (param.getCardId() == null || memberCardDao.selectById(param.getCardId()) == null) {
            throw new OrderInfoException(500,"会员卡ID无效或不存在");
        }
        MemberCard memberCard = memberCardDao.selectById(param.getCardId());
        if (memberCard == null) {
            throw new OrderInfoException(500,"会员卡ID无效或不存在");
        }

        memberCard.setCardName(param.getCardName());
        memberCard.setCardType(param.getCardType());
        memberCard.setCardLifespanDay(param.getCardLifespanDay());
        memberCard.setCardLifespanMonth(param.getCardLifespanMonth());
        memberCard.setCardPrice(param.getCardPrice());
        memberCard.setCardLimit(param.getCardLimit());

        memberCardDao.updateById(memberCard);
    }

    /**
     * 根据会员卡id查询
     * @param cardId
     * @return
     * @throws Exception
     */
    @Override
    public MemberCard getCardById(String cardId) throws Exception {
        MemberCard memberCard = memberCardDao.selectById(cardId);
        if (memberCard == null) {
            throw new IllegalArgumentException("会员卡ID无效");
        }
        return memberCard;
    }

    /**
     * 计算会员卡有效期
     *
     * @param cardId
     * @return
     */

    @Override
    public long calculateRemainingDays(String cardId) {
        MemberCard memberCard = getById(cardId);
        LocalDate today = LocalDate.now();
//        if (memberCard.getCardLifespanDay() != null) {
//            return ChronoUnit.DAYS.between(today, memberCard.getCardLifespanDay());
//        } else if (memberCard.getCardLifespanMonth() != null) {
//            return ChronoUnit.MONTHS.between(today, memberCard.getCardLifespanMonth()) * 30; // 粗略计算
//        }
        return 0;
    }

    @Override
    public PageInfo<MemberCard> load(LoadMemberCardParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<MemberCard> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getCardName())) {
            qw.like("card_name", param.getCardName());
        }
        if (ObjectUtil.isNotEmpty(param.getCardType())) {
            qw.eq("card_type", param.getCardType());
        }
        if (ObjectUtil.isNotEmpty(param.getMaxPrice())) {
            qw.le("card_price", param.getMaxPrice());
        }
        if (ObjectUtil.isNotEmpty(param.getMinPrice())) {
            qw.ge("card_price", param.getMinPrice());
        }
        List<MemberCard> memberCards = memberCardDao.selectList(qw);
        PageInfo<MemberCard> memberCardPageInfo = new PageInfo<>(memberCards);
        return memberCardPageInfo;
    }


}
